package com.sansecy.leanbacktest;

import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {
    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        FrameLayout frameLayout = new FrameLayout(parent.getContext());

        try {
            Class<?> aClass = Class.forName("androidx.leanback.widget.GridLayoutManager$LayoutParams");
            Constructor<?> constructor = aClass.getConstructor(int.class, int.class);
            constructor.setAccessible(true);
            Object object = constructor.newInstance(-1, -1);
            frameLayout.setLayoutParams((ViewGroup.LayoutParams) object);
        } catch (NoSuchMethodException | ClassNotFoundException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            e.printStackTrace();
        }

        frameLayout.setBackgroundColor(Color.RED);
        TextView itemView = new TextView(parent.getContext());
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(-1, -1);
        frameLayout.addView(itemView, layoutParams);
        MyHolder myHolder = new MyHolder(frameLayout);
        myHolder.TextView = itemView;
        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        ((TextView) holder.TextView).setText(position + "");
    }

    @Override
    public int getItemCount() {
        return 100;
    }

    class MyHolder extends RecyclerView.ViewHolder {
        TextView TextView;
        public MyHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
